package org.example.study;

import org.junit.Test;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestInetAddress {

    /**
     * @descriptions 生成IP对象
     * @param
     * @return
     */
    @Test
    public void test1() throws UnknownHostException {
        // 通过IP地址生成
        InetAddress byName = Inet4Address.getByName("192.168.68.151");
        System.out.println(byName); // /192.168.68.151

        // 通过域名生成，这种方式会去解析域名的IP，所以没有联网情况下会抛java.net.UnknownHostException
        InetAddress byName1 = Inet4Address.getByName("www.baidu.com");
        System.out.println(byName1); // www.baidu.com/14.215.177.38

        // 通过localhost域名生成
        InetAddress byName2 = Inet4Address.getByName("localhost");
        System.out.println(byName2); // localhost/127.0.0.1

        // 获取本机在局域网内的IP
        InetAddress localHost = Inet4Address.getLocalHost();
        System.out.println(localHost); // DESKTOP-BOE0R3E/192.168.136.1

    }
}
